package com.example.demo.controller;

import com.example.demo.Checker.interChecker;
import com.example.demo.DBmake.PostgreDB;
import com.example.demo.Encrypt.EncryptPwd;
import com.example.demo.Utils.TokenUtil;
import com.example.demo.api.CommonResult;
import com.example.demo.entity.UpdInfo;
import com.example.demo.entity.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class UpdController {
    @RequestMapping(value = "/upd",method = RequestMethod.POST)
    public CommonResult login(@RequestBody UpdInfo updInfo){

        if(updInfo.getToken()=="")  return CommonResult.unauthorized("登录过期");
        int uid = Integer.parseInt(updInfo.getUid());
        String token = updInfo.getToken();

        if(!TokenUtil.verify(token)) return CommonResult.unauthorized("登录过期");

        PostgreDB Target = new PostgreDB();

        int CK_NO = Target.CheckUserCom(uid);
        if(CK_NO == -1 ) return  CommonResult.validateFailed();

        String Img = updInfo.getImg();
//        System.out.println(Img);
        String nickname = updInfo.getNickname();
        String bir = updInfo.getBir();
        int sex = updInfo.getSex();

        if(CK_NO==1) {
            String sql = "INSERT INTO public.usercom VALUES ("+uid+",'[]','"+Img+"','"+nickname+"','"+bir+"',"+sex+")";
//            System.out.println(sql);
            if(!Target.updateItem(sql)) return CommonResult.validateFailed();
        }

        token = TokenUtil.sign(uid);
        return CommonResult.success(uid,token);
    }
}
